package com.itheima.d5_time;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class Test4Calendar {
    public static void main(String[] args) {
        // 目标：掌握Calendar的使用和特点。
        // 1、得到系统此刻时间对应的日历对象。
        Calendar calendar = Calendar.getInstance();
        //System.out.println("calendar = " + calendar);

        // 2、获取日历中的某个信息(年，月，日)
        //注意：get(Calendar.常量) 常量是用来区分要获取的时间信息
        int year = calendar.get(Calendar.YEAR);
        System.out.println("year = " + year);
        int month = calendar.get(Calendar.MONTH) + 1;
        System.out.println("month = " + month);
        int day = calendar.get(Calendar.DATE);
        System.out.println("day = " + day);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);
        int sec = calendar.get(Calendar.SECOND);
        System.out.println("hour = " + hour);
        System.out.println("min = " + min);
        System.out.println("sec = " + sec);


        // 3、拿到日历中记录的日期对象
        Date date = calendar.getTime();
        System.out.println("date = " + date);

        // 4、拿到时间毫秒值
        long timeInMillis = calendar.getTimeInMillis();
        System.out.println("timeInMillis = " + timeInMillis);

        // 5、修改日历中的某个信息
        //指定时间信息修改值
        calendar.set(Calendar.YEAR, 3033);
        System.out.println("calendar.get(Calendar.YEAR) = " + calendar.get(Calendar.YEAR));

        // 6、为某个信息增加或者减少多少
        //add(常量,值) 值可以是整数，也可以是负数
        //10000之后的日期信息
        Calendar now = Calendar.getInstance();
        showDatetime(now);//展示时间
        System.out.println("now = " + now.getTime());//Sun Feb 26 16:22:31 CST 2023
        now.add(Calendar.DATE,10000);
        Date time = now.getTime();
        System.out.println("time = " + time);//Thu Jul 14 16:22:31 CST 2050
        showDatetime(now);//展示时间


    }

    public static void showDatetime(Calendar calendar) {
        Date time = calendar.getTime();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println(sdf.format(time));
    }

}
